package com.tianzj.boot.mvc.service.system.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianzj.boot.mvc.entity.system.SysMenu;
import com.tianzj.boot.mvc.repository.system.SysMenuRepository;
import com.tianzj.boot.mvc.repository.system.SysRoleMenuRepository;
import com.tianzj.boot.mvc.service.system.SysMenuService;
import com.tianzj.boot.util.BeanCopyUtil;
import com.tianzj.boot.util.tree.BuildTree;
import com.tianzj.boot.util.tree.Tree;

@Service
public class SysMenuServiceImpl implements SysMenuService {
	
	@Autowired
	private SysMenuRepository sysMenuRepository;
	
	@Autowired
	private SysRoleMenuRepository sysRoleMenuRepository;
	
	@Override
	public void saveSysMenu(SysMenu sysMenu) {
		sysMenuRepository.save(sysMenu);
	}

	@Override
	public List<SysMenu> getSysMenuList() {
		return sysMenuRepository.findByStatusNot(9);
	}

	@Override
	public SysMenu findById(Long id) {
		return sysMenuRepository.findOne(id);
	}

	@Override
	public void updateSysMenu(SysMenu sysMenu) {
		SysMenu  menu= sysMenuRepository.findOne(sysMenu.getId());
		BeanUtils.copyProperties(sysMenu, menu, BeanCopyUtil.getNullPropertyNames(sysMenu));
		sysMenuRepository.save(menu);		
	}

	@Override
	public List<SysMenu> findByParentId(Long id) {
		return sysMenuRepository.findByParentIdEquals(id);
	}

	@Override
	public void deleteSysMenuById(Long id) {
		SysMenu sysMenu = sysMenuRepository.findOne(id);
		sysMenu.setStatus(9);
		sysMenuRepository.save(sysMenu);
	}

	@Override
	public Tree<SysMenu> getTree(Long id) {
		//查询所有的权限菜单不是删除状态和暂存状态的菜单
		List<SysMenu> sysMenus = sysMenuRepository.findByStatusEquals(1);
		//获取该角色所拥有的权限caidan
		List<Long> menuIds = sysRoleMenuRepository.findMenuIdByRoleId(id);
		List<Long> temp = menuIds;
		for (SysMenu menu : sysMenus) {
			if (temp!=null&&temp.size()>0&&temp.contains(new BigInteger(menu.getParentId().toString()))) {
				menuIds.remove(new BigInteger(menu.getParentId().toString()));
			}
		}
		List<Tree<SysMenu>> trees = new ArrayList<Tree<SysMenu>>();
		//List<SysMenu> SysMenus =getMeunsList();
		for (SysMenu menu : sysMenus) {
			Tree<SysMenu> tree = new Tree<SysMenu>();
			tree.setId(menu.getId().toString());
			tree.setParentId(menu.getParentId().toString());
			tree.setText(menu.getName());
			Map<String, Object> state = new HashMap<String,Object>();
			Long menuId = menu.getId();
			if (menuIds!=null&&menuIds.size()>0&&menuIds.contains(new BigInteger(menuId.toString()))) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<SysMenu> t = BuildTree.build(trees);
		return t;
	}

	@Override
	public SysMenu getMenuById(Long menuId) {
		return sysMenuRepository.findOne(menuId);
	}

	@Override
	public List<Tree<SysMenu>> listMenuTree(Long id) {
		List<Tree<SysMenu>> trees = new ArrayList<Tree<SysMenu>>();
		List<SysMenu> sysMenus= sysMenuRepository.findByUserId(id);
		for (SysMenu sysMenu : sysMenus) {
			Tree<SysMenu> tree = new Tree<SysMenu>();
			tree.setId(sysMenu.getId().toString());
			tree.setParentId(sysMenu.getParentId().toString());
			tree.setText(sysMenu.getName());
			Map<String, Object> attributes = new HashMap<String, Object>();
			attributes.put("url", sysMenu.getUrl());
			attributes.put("icon", sysMenu.getIcon());
			tree.setAttributes(attributes);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		List<Tree<SysMenu>> list = BuildTree.buildList(trees, "0");
		return list;
	}

}
